<?php

namespace app\index\controller;
use app\common\model\TmpUser;
use think\Request;
use app\common\model\Cars;
/**
* 
*/
class Custom extends Base
{
	
	public function index(){
		$request = Request::instance();
        if($request->isAjax()){
            $post = $request->post();
            $datemin = $post['datemin']??'';
            $datemax = $post['datemax']??'';
            $seach = $post['search_inpute']??''; 
            if( !empty($datemin) && !empty($datemax) && !empty($seach)){
            	$where = [
	                'user_name|id|minute'=>['like','%'.$seach.'%'],
	                'create_time'=>['between time',[$datemin,$datemax]]
	                ];
            }elseif( empty($datemin) && !empty($datemax) && !empty($seach)){
            	$where = [
                	'create_time'=>['<= time',$datemax],
                	'user_name|id|minute'=>['like','%'.$seach.'%']
                ];
            }elseif( !empty($datemin) && empty($datemax) && !empty($seach)){
            	$where = [
	                'create_time'=>['> time',$datemin],
                	'user_name|id|minute'=>['like','%'.$seach.'%']
	                ];
	        }elseif( !empty($datemin) && !empty($datemax) && empty($seach)){
	        	$where = [
	                'create_time'=>['between time',[$datemin,$datemax]]
	                ];
            }elseif( empty($datemin) && empty($datemax) && !empty($seach)){
            	$where = [
                'user_name|id|minute'=>['like','%'.$seach.'%']
                ];
            }elseif( !empty($datemin) && empty($datemax) && empty($seach)){
            	$where = [
                'create_time'=>['> time',$datemin]
                ];
            }elseif( empty($datemin) && !empty($datemax) && empty($seach)){
            	$where = [
                'create_time'=>['<= time',$datemax]
                ];
            }
            else{
	        	$where = '1=1';
	        }
            $model= TmpUser::where($where)
                            ->order('id')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);


            if(count($model)>=1){

                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                    $res['data']= '';
                    $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }
            
            return $res;

        }else{

            return $this->fetch();
        }

	}

	public function add(){
		$request = Request::instance();
		if($request->isAjax()){
			$data = $request->post();
			$car = Cars::get(['car_sn'=>$data['car_sn']]);
			if(!$car){
				$msg['status']=1;
				$msg['msg']='车牌号不存在,请查证后再添加';
				return $msg;
			}
			$data['car_id'] = $car['car_id'];
			
			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
			$model = new TmpUser();

			$result = $this->validate($data,
				    [
				        'user_name'  => 'unique:TmpUser'
				    ]);
			if(true !== $result){
				    return [
					   	'code'=>3,
						'msg'=>'用户名已经存在'
				    ];
			}
			try{
				$model->allowField(true)->save($data);
			}catch(\Exception $e){
				return [
					'code'=>2,
					'msg'=>$e->getMessage()
				];
			}
			return [
				'code'=>1,
				'msg'=>'添加成功'
			];
		}else{
			return $this->fetch();
		}
	}

	public function edit($id=1){
		$request = Request::instance();
		if($request->isAjax()){
			$data = $request->post();
			$car = Cars::get(['car_sn'=>$data['car_sn']]);
			if(!$car){
				$msg['status']=1;
				$msg['msg']='车牌号不存在,请查证后再添加';
				return $msg;
			}
			$data['car_id'] = $car['car_id'];
			$model = new TmpUser();
			try{

				$model->allowField(true)->isUpdate(true)->save($data);
				
			}catch(\Exception $e){
				return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}
			return [
                'code'=>1,
                'msg'=>'更新成功'
            ];			
		}else{
			$data = TmpUser::get($id);
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	public function password($id){
		$request = Request::instance();
		if($request->isAjax()){
			$data = $request->post();
			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);
			$model = new TmpUser();
			try{

				$model->allowField(true)->isUpdate(true)->save($data);

			}catch(\Exception $e){
				return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}
			return [
                'code'=>1,
                'msg'=>'更新成功'
            ];
		}else{
			$data = TmpUser::get($id);
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	public function delete($id){
		try{

			TmpUser::destroy($id);

		}catch(\Exception $e){
			return [
                'code'=>2,
                'msg'=>$e->getMessage()
            ];
		}
		return [
            'code'=>1,
            'msg'=>'删除成功'
        ];
	}
}